package com.lottery.ruok.lottrey.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.OfficalLotteryDetailActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.GetLotteryDetailResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.GetLotteryDetailResponse.LayoutsBean.ChildrenBeanX.ChildrenBean.SelectsBean;

import java.util.LinkedHashMap;
import java.util.List;

import ui.LotteryRowItem;

public class NewLotteryDetailAdapter extends BaseAdapter {
    private OfficalLotteryDetailActivity lotteryDetailActivity;
    private String layout_code;
    private LinkedHashMap max_odds;

    public NewLotteryDetailAdapter(OfficalLotteryDetailActivity lotteryDetailActivity, String layout_code, LinkedHashMap max_odds){
        this.lotteryDetailActivity = lotteryDetailActivity;
        setDatas(layout_code, max_odds);
    }

    public void setDatas(String layout_code, LinkedHashMap max_odds){
        this.layout_code = layout_code;
        this.max_odds = max_odds;
        clearSelectTab();
    }

    private void clearSelectTab(){
        List<SelectsBean> lists =  lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().get(lotteryDetailActivity.typePosition3).getSelects();
        for(SelectsBean selectsBean : lists){
            selectsBean.setSelectLabel(0);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(lotteryDetailActivity).inflate(R.layout.lottery_play_item, viewGroup, false);
            holder = new ViewHolder();
            holder.lotteryRowItem = view.findViewById(R.id.item);
            view.setTag(holder);
        }

        final SelectsBean selectsBean =  getItem(i);
        holder.lotteryRowItem.clearContent();
        holder.lotteryRowItem.setSelectInter(new LotteryRowItem.SelectTabInter(){

            @Override
            public void onSelectTab(int tab) {
                selectsBean.setSelectLabel(tab);
            }
        });
        if(getItem(i) != null && lotteryDetailActivity.chosenItems.size() > i) {
            holder.lotteryRowItem.setTotal(selectsBean.getSelectLabel(), selectsBean.getContent(), lotteryDetailActivity.chosenItems.get(i), i, layout_code, max_odds);
        }
        holder.lotteryRowItem.resetTabState();
        holder.lotteryRowItem.setChange(null);
        holder.lotteryRowItem.setChange(new LotteryRowItem.Change() {
            @Override
            public void change(int row, int column) {
                //监听chosenItems变化
                lotteryDetailActivity.specialHandle(row, column);

                lotteryDetailActivity.listenToChange();
            }
        });
        if(getItem(i) != null) {
            if (getItem(i).getTools().equals("1")) {
                holder.lotteryRowItem.hasTools(true);
            } else {
                holder.lotteryRowItem.hasTools(false);
            }
            holder.lotteryRowItem.setUnit(getItem(i).getLabel());
        }

        return view;
    }

    static class ViewHolder {
        LotteryRowItem lotteryRowItem;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public GetLotteryDetailResponse.LayoutsBean.ChildrenBeanX.ChildrenBean.SelectsBean getItem(int i) {
        if(lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().get(lotteryDetailActivity.typePosition3).getSelects() != null
                && i < lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().get(lotteryDetailActivity.typePosition3).getSelects().size()){
            return lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().get(lotteryDetailActivity.typePosition3).getSelects().get(i);
        } else {
            return null;
        }
    }

    @Override
    public int getCount() {
        if(lotteryDetailActivity.getLotteryDetailResponse.getLayouts() != null
                && lotteryDetailActivity.getLotteryDetailResponse.getLayouts().size() > lotteryDetailActivity.typePosition1
                && lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren() != null
                && lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().size() > lotteryDetailActivity.typePosition2
                && lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren() != null
                && lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().size() > lotteryDetailActivity.typePosition3){
            if (lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().get(lotteryDetailActivity.typePosition3).getSelects() == null) {
                return 0;
            }
            return lotteryDetailActivity.getLotteryDetailResponse.getLayouts().get(lotteryDetailActivity.typePosition1).getChildren().get(lotteryDetailActivity.typePosition2).getChildren().get(lotteryDetailActivity.typePosition3).getSelects().size();
        } else {
            return 0;
        }
    }
}